//
//  MyActivityViewController.m
//  UI_豆瓣
//
//  Created by dllo on 15/11/2.
//  Copyright © 2015年 蓝鸥科技. All rights reserved.
//

#import "MyActivityViewController.h"
#import "DataBaseSingleng.h"
#import "ActivitiesDetailViewController.h"
@interface MyActivityViewController ()<UITableViewDataSource,UITableViewDelegate>

@property(nonatomic, retain)UITableView *myTableView;

@property (nonatomic, retain)NSMutableArray *allArray;

@end

@implementation MyActivityViewController

- (void)dealloc
{
    [_allArray release];
    [_myTableView release];
    [super dealloc];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor cyanColor];
    
    self.myTableView = [[UITableView alloc]initWithFrame:[[UIScreen mainScreen]bounds]];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    [_myTableView release];
    
    
    [self.myTableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"myActivity"];
    
    
    NSString *name = [[NSUserDefaults standardUserDefaults] objectForKey:@"userName"];
    self.allArray = [[DataBaseSingleng shareDataBase]selectMyActvity:name];

    
}

#pragma mark -- 设置行
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    
    return self.allArray.count;
}

#pragma mark -- 设置cell
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static  NSString *cellmyActivity = @"myActivity";
    UITableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:cellmyActivity];
    
    
    cell.textLabel.text = [self.allArray objectAtIndex:indexPath.row];
    
    return cell;
}

#pragma mark -- 触摸cell时候触发
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    //点中置灰  放开取消置灰
    [tableView deselectRowAtIndexPath:indexPath animated:YES];

    ActivitiesDetailViewController *actDetaVC = [[ActivitiesDetailViewController alloc]init];
    
    NSString *title = [self.allArray objectAtIndex:indexPath.row];
    ActivitesList *act = [[ActivitesList alloc]init];
    NSMutableArray *array = [[DataBaseSingleng shareDataBase]selectActivity:title];
    for (act in array) {
        NSLog(@"==%@",act);
         actDetaVC.actList = act;
    }
    
    
    
    //隐藏详情界面的TabBar
    [actDetaVC setHidesBottomBarWhenPushed:YES];
    
    [self.navigationController pushViewController:actDetaVC animated:YES];
}


#pragma mark -- 每行的编辑样式(删除/添加)

-(UITableViewCellEditingStyle)tableView:(UITableView *)tableView editingStyleForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    return UITableViewCellEditingStyleDelete;
}


#pragma mark -- 实现编辑逻辑

- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
    //如果是进行删除操作
    if (editingStyle == UITableViewCellEditingStyleDelete) {
        
        //特别注意:一定要先删除数据 后删cell
        NSString *title =  [self.allArray objectAtIndex:indexPath.row];
        
        NSString *name = [[NSUserDefaults standardUserDefaults]objectForKey:@"userName"];
        
        
        //特别注意  一定是先删数据, 后删cell
        [[DataBaseSingleng shareDataBase]deleteMyActivityTable:title name:name];
        
        [self.allArray removeObjectAtIndex:indexPath.row];
        //删除cell
        [tableView deleteRowsAtIndexPaths:[NSArray arrayWithObjects:indexPath, nil] withRowAnimation:UITableViewRowAnimationFade];
        

        
    }
}





- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
